package com.usian.reggie.controller;

import com.github.pagehelper.PageInfo;
import com.usian.reggie.common.R;
import com.usian.reggie.entity.Dish;
import com.usian.reggie.entity.Employee;
import com.usian.reggie.entity.dto.DishDto;
import com.usian.reggie.entity.dto.SetmealDto;
import com.usian.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;


    /**
     * 保存菜品信息
     *
     * @param dishDto
     * @param session
     * @return
     */
    @PostMapping()
    public R saveDish(@RequestBody DishDto dishDto, HttpSession session) {

        Employee employee1 = (Employee) session.getAttribute("user");

        if(dishDto.getId()==null){
            //新增
            if (employee1 != null) {
                dishDto.setCreateUser(employee1.getId());
                dishDto.setUpdateUser(employee1.getId());
            }
            dishService.save(dishDto);
        }else{
            //修改
            if (employee1 != null) {
                dishDto.setUpdateUser(employee1.getId());
            }
            dishService.update(dishDto);
        }
        return R.success("保存成功");
    }

    /**
     * 分页查询菜品列表
     *
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R page(@RequestParam(defaultValue = "1") Integer page,
                  @RequestParam(defaultValue = "10")Integer pageSize,
                  String name){

        PageInfo<DishDto> pageInfo = dishService.page(page, pageSize, name);
        return R.success(pageInfo);
    }

    /**
     * 修改回显
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R findDishById(@PathVariable Long id){
        DishDto dishDto = dishService.findById(id);
        return R.success(dishDto);
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/del")
    public R del(String ids){
        String[] strings = ids.split(",");
        for(String s :strings){
            dishService.del(Long.parseLong(s));
        }
        return R.success("删除成功");
    }


    /**
     * 套餐列表中  根据分类id查询菜品列表
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public R list(Long categoryId){
        List<Dish> dishList = dishService.findByCategoryId(categoryId);
        return R.success(dishList);
    }

    /**
     * 启售、批量启售、停售、批量停售
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R status(@PathVariable Integer status,String ids,HttpSession session){
        Employee employee = (Employee) session.getAttribute("user");
        String[] strings = ids.split(",");
        for(String id:strings){
            DishDto dishDto = new DishDto();
            dishDto.setId(Long.parseLong(id));
            dishDto.setStatus(status);
            if(employee!=null){
                dishDto.setUpdateUser(employee.getId());
            }
            dishService.update(dishDto);
        }
        return R.success("修改成功");
    }
}
